package com.graduation_design.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation_design.entity.AppAd;
import com.graduation_design.entity.AppCategory;
import com.graduation_design.entity.AppProduct;
import com.graduation_design.service.AppAdService;
import com.graduation_design.service.AppCategoryService;
import com.graduation_design.service.AppHomeService;
import com.graduation_design.service.AppProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AppHomeServiceImpl implements AppHomeService {

    @Resource
    AppAdService appAdService;

    @Resource
    AppCategoryService appCategoryService;

    @Resource
    AppProductService appProductService;

    @Override
    public Object getContents() {
        // 通过mybatis plus的list接口，获取各种各样的数据，需要注意一下排序。

        // 轮播图
        List<AppAd> carousels = appAdService.list(new QueryWrapper<AppAd>()
                .eq("position", "carousel")
                .eq("is_show", true)
                .orderByDesc("sort_order")
        );

        // 分类
        List<AppCategory> categories = appCategoryService.list(new QueryWrapper<AppCategory>()
                .eq("is_show", true)
                .orderByDesc("sort_order")
        );

//        // 热销产品
//        List<AppProduct> hots = appProductService.list(new QueryWrapper<AppProduct>()
//                .eq("is_hot", true)
//                .eq("is_on_sale", true)
//                .orderByDesc("sort_order")
//                .last("limit 10")
//        );
//
//        // 置顶产品
//        List<AppProduct> news = appProductService.list(new QueryWrapper<AppProduct>()
//                .eq("is_top", true)
//                .eq("is_on_sale", true)
//                .orderByDesc("sort_order")
//                .last("limit 10")
//        );

        // 置顶或热销产品
        List<AppProduct> products = appProductService.list(new QueryWrapper<AppProduct>()
                .and(i -> i.eq("is_hot", true).or().eq("is_top", true))
                .eq("is_on_sale", true)
                .orderByDesc("sort_order")
                .last("limit 10")
        );

        return MapUtil.builder()
                .put("carousels", carousels)
                .put("categories", categories)
                .put("products", products)
                .build();
    }
}
